package dearchenpush.good.com.qianfanlive.modules.live.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import dearchenpush.good.com.qianfanlive.R;
import dearchenpush.good.com.qianfanlive.url.UrlConfigs;

/**
 * A simple {@link Fragment} subclass.
 */
public class LiveFragment extends Fragment {


    private HorizontalScrollView scrollView;
    private LinearLayout linearLayout;
    private ViewPager viewPager;
    private String[] title;
    private List<Fragment> data;
    private List<TextView> textViews;

    private SwipeRefreshLayout refreshLayout;

    public LiveFragment() {
        // Required empty public constructor
    }
    public static LiveFragment newInstance(){
        LiveFragment fragment = new LiveFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_live, container, false);
        initView(view);
        initData();
        initTextView();
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
        setlistener();
        return view;
    }

    private void setlistener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                refreshLayout.setRefreshing(false);

            }
        });

    }

    private void initTextView() {
        for (int i = 0; i < title.length; i++) {
            TextView textView = new TextView(getContext());
            textView.setEnabled(true);
            textView.setText(title[i]);
            textView.setTextColor(Color.BLACK);
            textView.setPadding(35,10,35,35);
            textView.setTag(i);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i = (int) v.getTag();
                    viewPager.setCurrentItem(i);
                }
            });
            textViews.add(textView);
            linearLayout.addView(textView);
        }
        textViews.get(0).setTextColor(Color.parseColor("#FFCB9D65"));
        textViews.get(0).setEnabled(false);
    }



    private void initData() {
        data = new ArrayList<>();

        title = new String[]{"全部","高颜值","萌妹子","好声音","有才艺","小鲜肉","逗B/MC","劲爆","还有更多"};
        for (int i = 0; i < title.length; i++) {
            Bundle bundle = new Bundle();
            bundle.putString("url", String.format(UrlConfigs.LIVE_URL,0,i));
            bundle.putInt("page",i);
            LiveInFragment inFragment = new LiveInFragment();
            inFragment.setArguments(bundle);
            data.add(inFragment);
        }
    }

    private void initView(View view) {
        scrollView = (HorizontalScrollView) view.findViewById(R.id.fragment_live_horizonId);
        textViews = new ArrayList<>();
        linearLayout = (LinearLayout) view.findViewById(R.id.fragment_live_linear);
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.fragment_live_swiperefresh);
        viewPager = (ViewPager) view.findViewById(R.id.fragment_live_pager);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {

                for (int i = 0; i < textViews.size(); i++) {
                    textViews.get(i).setEnabled(true);
                    textViews.get(i).setTextColor(Color.parseColor("#FF0F0E0E"));
                }
                textViews.get(position).setEnabled(false);
                textViews.get(position).setTextColor(Color.parseColor("#FFCB9D65"));
                int widthPinxels = getResources().getDisplayMetrics().widthPixels;
                TextView textView = textViews.get(position);
                int offset = textView.getLeft() + textView.getWidth() / 2 - widthPinxels / 2;
                scrollView.smoothScrollTo(offset,0);




            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



    }

    class MyAdapter extends FragmentStatePagerAdapter {


        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }

        @Override
        public int getCount() {
            return data!=null?data.size():0;
        }
    }

}
